﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TinyMoocSite.Models;
using TinyMoocSite.DataAccessLayer;
using TinyMoocSite.ViewModels;
using TinyMoocSite.Filters;

namespace TinyMoocSite.Helpers
{
    public class UserInfoHelper
    {
        public UserInfo GetUserInfoByUsername(string username)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            List<UserInfo> userInfoList = dal.UserInfo.Where(q => q.Username == username).ToList();
            if (userInfoList.Count == 0)
                return null;
            return userInfoList[0];
        }

        public bool ValidateUserLogin(string username, string encryptedPassword)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            List<UserInfo> userInfoList = dal.UserInfo.Where(
                q => q.Username == username && q.EncryptedPassword == encryptedPassword
            ).ToList();

            return (userInfoList.Count == 1);
        }

        public UserPrivilege GetUserPrivilege(string username)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            List<UserInfo> userInfoList = dal.UserInfo.Where(q => q.Username == username).ToList();
            return userInfoList[0].Privilege;
        }

        public void InsertUserInfo(RegisterForm register)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            UserInfo newEntry = new UserInfo();
            newEntry.Username = register.Username;
            newEntry.FullName = register.FullName;
            newEntry.EncryptedPassword = register.Password;
            newEntry.LicenseId = register.LicenseId;
            newEntry.Privilege = UserPrivilege.Student;
            newEntry.LastActivityDate = DateTime.Now;
            newEntry.RegisterDate = DateTime.Now;
            newEntry.Website = "";
            newEntry.ProfTitle = "";
            newEntry.Email = "";
            newEntry.Memo = "";
            dal.UserInfo.Add(newEntry);
            dal.SaveChanges();
        }

        public void InsertUserInfo(NewTeacherForm register)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            UserInfo newEntry = new UserInfo();
            newEntry.Username = register.Username;
            newEntry.FullName = register.MyFullName;
            newEntry.EncryptedPassword = register.Password;
            newEntry.LicenseId = register.MyLicenseId;
            newEntry.Website = register.Website;
            newEntry.ProfTitle = register.ProfTitle;
            newEntry.Email = register.Email;
            newEntry.Memo = "";
            newEntry.Privilege = UserPrivilege.Teacher;
            newEntry.LastActivityDate = DateTime.Now;
            newEntry.RegisterDate = DateTime.Now;
            // others are blacked.
            dal.UserInfo.Add(newEntry);
            dal.SaveChanges();
        }

        public void RefreshLoginDate(string username)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            List<UserInfo> userInfoList = dal.UserInfo.Where(q => q.Username == username).ToList();
            if (userInfoList.Count == 0)
                return;

            UserInfo userInfo = userInfoList[0];
            userInfo.LastActivityDate = DateTime.Now;

            dal.SaveChanges();
        }

        public void UpdateUserInfo(UserDetailForm userDetail)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            List<UserInfo> userInfoList = dal.UserInfo.Where(q => q.Username == userDetail.Username).ToList();
            if (userInfoList.Count == 0)
                return;

            UserInfo userInfo = userInfoList[0];

            userInfo.Email = userDetail.Email;
            userInfo.Memo = userDetail.Memo;
            userInfo.ProfTitle = userDetail.ProfTitle;
            userInfo.Website = userInfo.Website;

            dal.SaveChanges();
        }

        public void ListUserInfo(AccountOverview overview)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            List<UserInfo> userInfoList = dal.UserInfo.ToList();
            overview.AccountList = new List<AccountShortItem>();

            foreach (UserInfo user in userInfoList)
            {
                AccountShortItem shortItem = new AccountShortItem();
                shortItem.Username = user.Username;
                shortItem.FullName = user.FullName;
                shortItem.LicenseId = user.LicenseId;
                shortItem.Privilege = user.Privilege.ToString();
                shortItem.RegisterDate = user.RegisterDate.ToLongDateString();

                overview.AccountList.Add(shortItem);
            }
        }

        public void UpdatePassword(string userName, ChangePasswordForm form)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            List<UserInfo> userInfoList = dal.UserInfo.Where(q => q.Username == userName).ToList();
            if (userInfoList.Count == 0)
                return;

            UserInfo userInfo = userInfoList[0];
            userInfo.EncryptedPassword = form.NewPassword;
            dal.SaveChanges();
        }

        public StudentListVM FillStudentEnrollingList(string courseId)
        {
            TinyMoocDAL dal = new TinyMoocDAL();
            List<CourseInfo> courseInfoList =
                dal.CourseInfo.Include("FollowedUserList").Where(p => p.Id.ToString() == courseId).ToList();
            if (courseInfoList.Count != 1)
                return null;
            List<UserInfo> userInfoList =
                courseInfoList[0].FollowedUserList.Where(p => p.Privilege == UserPrivilege.Student).ToList();
            StudentListVM studentList = new StudentListVM();
            studentList.StudentList = new List<StudentShortItem>();
            studentList.Id = courseInfoList[0].Id;
            studentList.Title = courseInfoList[0].Title;
            studentList.StartDate = courseInfoList[0].StartDate.ToLongDateString();
            studentList.EndDate = courseInfoList[0].EndDate.ToLongDateString();

            foreach (UserInfo user in userInfoList)
            {
                studentList.StudentList.Add(new StudentShortItem
                {
                    Username = user.Username,
                    FullName = user.FullName,
                    Email = user.Email,
                    LicenseId = user.LicenseId,
                    LastActivityDate = user.LastActivityDate.ToLongDateString()
                });
            }

            return studentList;
        }
    }
}